
package com.yyfax.ph.common.scan.idcard.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.yyfax.ph.R;
import com.yyfax.ph.common.scan.idcard.CameraManager;

public final class ViewfinderView extends View {
	private final static long ANIMATION_DELAY = 10L;
	private int mLineStep = 0;
	private Rect mLineRect;
	private Drawable mLineDrawable;
	private int LINE_HEIGHT = 0;
	private int LINE_SPEED = 5;
	private final Paint mPaint;
	private final int mFrameColor;
	private int mTipColor;
	private float mTipTextSize;
	private String mTipText;
	private int mLineWidth = 4;
	private int mRoundWidth = 40;

	public ViewfinderView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setWillNotDraw(false);
		mPaint = new Paint();
		//mFrameColor = R.color.color_00ff00;
		Resources resources = getResources();
		mFrameColor = resources.getColor(R.color.color_2b92ec);
		mTipColor = resources.getColor(R.color.color_ffffff);
		mTipTextSize = getResources().getDisplayMetrics().density * 15;
		mLineWidth = (int)(getResources().getDisplayMetrics().density * 2);
		mRoundWidth = (int)(getResources().getDisplayMetrics().density * 20);
		mLineRect = new Rect();
		int lineDrawableId = R.drawable.scan_line_landscape;
		mLineDrawable = context.getResources().getDrawable(lineDrawableId);
		LINE_HEIGHT = mLineDrawable.getIntrinsicHeight();
	}

	@Override
	public void onDraw(Canvas canvas) {
		Rect frame = CameraManager.get().getFramingRect();
		if (frame == null) {
			return;
		}
		int width = canvas.getWidth();
		int height = canvas.getHeight();
		if ((mLineStep += LINE_SPEED) < height - LINE_HEIGHT) {
			canvas.save();
			mLineRect.set(0, mLineStep, width, mLineStep + LINE_HEIGHT);
			mLineDrawable.setBounds(mLineRect);
			mLineDrawable.draw(canvas);
			canvas.restore();
		} else {
			mLineStep = 0;
		}
		canvas.save();
		mPaint.setColor(mFrameColor);
		canvas.drawRect(0, 0, mRoundWidth, mLineWidth, mPaint);
		canvas.drawRect(0, 0, mLineWidth, mRoundWidth, mPaint);
		canvas.drawRect(width - mRoundWidth, 0, width, mLineWidth, mPaint);
		canvas.drawRect(width - mLineWidth, 0, width, mRoundWidth, mPaint);
		canvas.drawRect(0, height - mLineWidth, mRoundWidth, height, mPaint);
		canvas.drawRect(0, height - mRoundWidth, mLineWidth, height, mPaint);
		canvas.drawRect(width - mRoundWidth, height - mLineWidth, width, height, mPaint);
		canvas.drawRect(width - mLineWidth, height - mRoundWidth, width, height, mPaint);
		//绘制提示文字
		if (!TextUtils.isEmpty(mTipText)) {
			mPaint.setTextAlign(Paint.Align.CENTER);
			mPaint.setColor(mTipColor);
			mPaint.setTextSize(mTipTextSize);
			canvas.translate(width / 2, frame.top + frame.height() / 10);
			canvas.drawText(mTipText, 0, 0, mPaint);
		}
		canvas.restore();
		postInvalidateDelayed(ANIMATION_DELAY, frame.left, frame.top, frame.right, frame.bottom);
	}

	public void setTipText(String str) {
		this.mTipText = str;
	}

	public void setTipColor(int color) {
		this.mTipColor = color;
	}
}
